def use_cnt(api_ls, mash_ls, rel_ls):
    api_use_cnt = {}
    for mash in mash_ls.keys():
        if mash in rel_ls.keys():
            for api in rel_ls[mash]:
                if api in api_ls.keys():
                    if api in api_use_cnt:  # 已存在则 +1
                        api_use_cnt[api] += 1
                    else:
                        api_use_cnt[api] = 1  # 不存在则置 1
    return api_use_cnt


def prov_cnt(api_ls, mash_ls, rel_ls):
    api_prov = {}
    for mash in mash_ls.keys():
        if mash in rel_ls.keys():
            for api in rel_ls[mash]:
                if api in api_ls.keys():
                    name = ''
                    for i in range(5, len(api)):  # /api/xxx -> xxx
                        if api[i] == '-':
                            break
                        name += api[i]
                    if name in api_prov.keys():  # 已包含该提供商
                        api_prov[name] += 1
                    else:
                        api_prov[name] = 1
    return api_prov


def category_api(api_ls):
    cate_api = {}
    for api in api_ls.keys():
        cate = api_ls[api][2]
        if cate in cate_api.keys():
            cate_api[cate].append(api)  # 如果包含此类别，则将 API 地址加入
        else:
            cate_api[cate] = [api]
    return cate_api


def category_mash_api(api_ls, api_used_ls):
    cate_mash_api = {}
    for api in api_ls.keys():
        if api in api_used_ls:
            cate = api_ls[api][2]
            if cate in cate_mash_api.keys():
                cate_mash_api[cate].append(api)  # 如果包含此类别，则将 API 地址加入
            else:
                cate_mash_api[cate] = [api]
    return cate_mash_api


def api_type_set_cnt(api_ls, mash_ls, rel_ls):
    type_set = {}  # 统计每种 API 组合出现的次数
    for mash in mash_ls.keys():
        if mash in rel_ls.keys():
            temp_set = set()
            for api in rel_ls[mash]:
                if api in api_ls.keys():
                    temp_set.add(api_ls[api][1])
            froze_set = frozenset(temp_set)
            if len(froze_set) > 0:
                if froze_set in type_set.keys():
                    type_set[froze_set] += 1
                else:
                    type_set[froze_set] = 1
    return type_set
